﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Drawing.Imaging;

namespace DevExpress.MailClient.Win.Controls.File_tabs.File_Viewers
{
    public partial class ucTiffFileViewer : DevExpress.XtraEditors.XtraUserControl
    {
        public ucTiffFileViewer()
        {
            InitializeComponent();
        }

        public string FILE_PATH
        {
            set { LoadPage(value); }
        }

        #region Image Viewer Controls

        public string CurrentImagePath { get; set; }
        MODI.Document _MODIDocument = null;

        private void LoadPage(string filePath)
        {
            imagingForWindows1.OPEN_FILE(filePath);
        }

        private void btnRotateLeft_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.ROTATE_LEFT);
        }

        private void btnRotateRight_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.ROTATE_RIGHT);

        }

        private void btnOCR_Click(object sender, EventArgs e)
        {


        }

        private void btnRemoveNoise_Click(object sender, EventArgs e)
        {
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            try
            {
                imagingForWindows1.SET_ZOOM_OPTION(ImagingController.ImagingForWindows.ZoomOptions.ZoomIn, 10);
            }
            catch { }
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            try
            {
                imagingForWindows1.SET_ZOOM_OPTION(ImagingController.ImagingForWindows.ZoomOptions.ZoomOut, 10);
            }
            catch { }
        }

        public Image ScaleImage(Image image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }

        public Bitmap ScaleImage(Bitmap image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);

            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return new Bitmap(newImage);
        }
        private void btnCropImage_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.CROP_SELECTED_REGION);
        }


        private void btnBestFit_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_FIT_TO_VIEW(ImagingController.ImagingForWindows.FitTo.BEST_FIT);
        }

        private void btnAdjustImage_Click(object sender, EventArgs e)
        { imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.DESQUE_PAGE);
        }

        private void btnSelectImage_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.SELECT_ANNOTATIONS);
        }

        #endregion Image Viewer Controls

        public ucTiffFileViewer(string filePath)
        {
            InitializeComponent();
            FILE_PATH = filePath;
        }

        private void tsbSaveChanges_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SAVE_BURN_ANNOTATIONS();
        }

        private void tsbRedoChanges_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.REDO_CHANGES);
        }

        private void tsbUndoChanges_Click(object sender, EventArgs e)
        {
            imagingForWindows1.SET_CURRENT_ANNOTATION_TOOL(ImagingController.ImagingForWindows.ToolSettings.UNDO_CHANGES);
        }
    }
}
